package com.nvc.homeplan;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.text.Html;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.nvc.homeplan.data.vo.*;


public class PlanListAdapterPlanBook  extends BaseAdapter 
{
    private static final String CLASSTAG = PlanListAdapter.class.getSimpleName();
    private final Context context;
    private final List<PlanVO> plans;
    private LayoutInflater layoutInflater;
    private final ImageDownloader imageDownloader = new ImageDownloader();

    public PlanListAdapterPlanBook(Context context, List<PlanVO> plans) {
        this.context = context;
        this.plans = plans;
        Log.v(Constants.LOGTAG, " " + PlanListAdapterPlanBook.CLASSTAG + " reviews size - " + this.plans.size());
        layoutInflater = (LayoutInflater) this.context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    }

    public int getCount() {
        return this.plans.size();
    }

    public Object getItem(int position) {
        return this.plans.get(position);
    }

    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) 
    {
        PlanVO plan = this.plans.get(position);

    	ViewHolder viewHolder = null;
    	
    	TextView title = null;
    	ImageView img=null;
    	
    	if (null == convertView)
    	{
            convertView = layoutInflater.inflate(R.layout.plan_list_single_item_plan_book, null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
    	}
    	viewHolder = (ViewHolder) convertView.getTag();
    	
    	String htmlText = "";
    	if (plan.retailPrice != plan.price)
    	{
        	htmlText += "Retail Price: <strike>" + plan.retailPrice + "</strike><br>";
        	htmlText += "Special Price: <b>";
        	if (plan.price == 0)
        		htmlText += "FREE";
        	else
        		htmlText += plan.price;
        	htmlText += "</b>";
    	}
    	else
    	{
    		htmlText += "Price : " + plan.price;
    	}
    	htmlText += "<br/>";
    	if (plan.freeShipping)
    		htmlText += "<b>FREE</b>";
    	else
    		htmlText += plan.priceBaseShipping;
    	
    	htmlText += " Ground Shipping (U.S Only)";

    	title = viewHolder.gettitle();
    	title.setText(Html.fromHtml(htmlText));


        	
        img=viewHolder.getImage();
        imageDownloader.download(plan.imageURL, (ImageView) img);
         
        return convertView;
    }
    
    private class ViewHolder 
    {
    	private View listRow;
        private TextView title = null;
        private ImageView i11=null; 
        
        public ViewHolder(View listRow) 
        {
        	this.listRow = listRow;
        }
        
        public TextView gettitle() 
        {
        	if(null == title)
        	{
        		title = (TextView) listRow.findViewById(R.id.title);
            }
            return title;
         }     
         
         public ImageView getImage() 
         {
        	 if(null == i11){
                  i11 = (ImageView) listRow.findViewById(R.id.img);
        	 }
             return i11;
        }
    }
}